package com.xywl.escort.view.tips;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.makeramen.roundedimageview.RoundedImageView;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.xywl.escort.R;
import com.xywl.escort.base.BaseFragment;
import com.xywl.escort.bean.UserFundInfoResp;
import com.xywl.escort.utils.RxSPTool;
import com.xywl.escort.utils.ToastUtil;
import com.xywl.escort.widget.GlideApp;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import dagger.android.support.AndroidSupportInjection;
import qiu.niorgai.StatusBarCompat;
import timber.log.Timber;

import static com.xywl.escort.common.PreferenceKeys.HEAD_IMG;
import static com.xywl.escort.common.PreferenceKeys.NICK_NAME;
import static com.xywl.escort.common.PreferenceKeys.PHONE;
import static com.xywl.escort.common.PreferenceKeys.TOKEN;
import static com.xywl.escort.common.PreferenceKeys.USER_ID;

public class TipsFragment extends BaseFragment implements TipsContract.View {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.text_nickname)
    TextView mTextNickname;
    @BindView(R.id.text_balance)
    TextView mTextBalance;
    @BindView(R.id.text_funds)
    TextView mTextFunds;
    @BindView(R.id.text_income)
    TextView mTextIncome;
    @BindView(R.id.img_avatar)
    RoundedImageView mImgAvatar;

    @Inject
    TipsContract.Presenter mPresenter;

    public TipsFragment() {
        // Required empty public constructor
    }

    public static TipsFragment newInstance(String param1) {
        TipsFragment fragment = new TipsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_tips;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        if (null != this.getActivity()) {
            StatusBarCompat.translucentStatusBar(getActivity(), true);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Timber.tag("TipsFragment").e("onResume");
        if (null != this.getActivity()) {
            String nickName = RxSPTool.getString(this.getActivity(), NICK_NAME);
            if (!TextUtils.isEmpty(nickName)) {
                mTextNickname.setText(nickName);
            } else {
                String phone = RxSPTool.getString(this.getActivity(), PHONE);
                if (!TextUtils.isEmpty(phone)) {
                    mTextNickname.setText(phone);
                } else {
                    mTextNickname.setText("未登录");
                }
            }
            String headImg = RxSPTool.getString(this.getActivity(), HEAD_IMG);
            if (!TextUtils.isEmpty(headImg)) {
                GlideApp.with(this.getActivity())
                        .load(headImg)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)
                        .placeholder(R.mipmap.icon_default_picture)
                        .into(mImgAvatar);
            } else {
                GlideApp.with(this.getActivity())
                        .load(R.mipmap.icon_default_picture)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)
                        .placeholder(R.mipmap.icon_default_picture)
                        .into(mImgAvatar);
            }

            mPresenter.takeView(this);
            String userId = RxSPTool.getString(this.getActivity(), USER_ID);
            if (!TextUtils.isEmpty(userId)) {
                mPresenter.getUserFunds(userId);
            } else {
                mTextBalance.setText(R.string.tips_default_balance);
                mTextFunds.setText(R.string.tips_default_balance);
                mTextIncome.setText(R.string.tips_default_balance);
            }
        }
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.layout_info, R.id.text_setting, R.id.text_address, R.id.layout_coupon,
            R.id.text_purchase, R.id.text_subscription, R.id.text_record, R.id.text_withdraw})
    public void onViewClicked(View view) {
        String token = "";
        if (null != this.getActivity()) {
            token = RxSPTool.getString(this.getActivity(), TOKEN);
        }
        switch (view.getId()) {
            case R.id.layout_info: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/user/info").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.text_setting: {
                ARouter.getInstance().build("/xy/setting").navigation();
            }
            break;
            case R.id.text_address: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/address/manage").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.text_purchase: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/order/purchase").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.text_subscription: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/order/subscription").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.text_record: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/fund/records").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.layout_coupon: {
                if (!TextUtils.isEmpty(token)) {
                    ARouter.getInstance().build("/xy/coupon").navigation();
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
            }
            break;
            case R.id.text_withdraw: {
                if (!TextUtils.isEmpty(token)) {
                    if (null != this.getActivity()) {
                        String userId = RxSPTool.getString(this.getActivity(), USER_ID);
                        mPresenter.isSetPayPwd(userId);
                    }
                } else {
                    ARouter.getInstance().build("/xy/login").navigation();
                }
                // ARouter.getInstance().build("/xy/guide").navigation();
            }
            break;
            default:
                break;
        }
    }

    @Override
    public void getUserFundsSuccess(UserFundInfoResp.DataBean dataBean) {
        if (!TextUtils.isEmpty(dataBean.totalAssetsYuan)) {
            mTextBalance.setText(dataBean.totalAssetsYuan);
        }
        if (!TextUtils.isEmpty(dataBean.accountFundsYuan)) {
            mTextFunds.setText(dataBean.accountFundsYuan);
        }
        if (!TextUtils.isEmpty(dataBean.totalIncomeYuan)) {
            mTextIncome.setText(dataBean.totalIncomeYuan);
        }
    }

    @Override
    public void getUserFundsFailure() {
        Timber.e("getUserFundsFailure");
    }

    @Override
    public void hasSetPayPwd() {
        ARouter.getInstance().build("/xy/withdraw").navigation();
    }

    @Override
    public void noSetPayPwd() {
        ARouter.getInstance().build("/xy/pwd/setting/pay").navigation();
    }

    @Override
    public void findIsFitPayPwdFailure(String msg) {
        if (null != this.getActivity()) {
            ToastUtil.showTip(this.getActivity(), msg);
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
